package com.tky.questions.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.tky.questions.R;

/**
 * Created by wuxuan on 2016/4/11.
 */
public abstract class BaseActivity extends ActionBarActivity {

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 禁止旋转

//        initActionBar("问题库", true, false, R.mipmap.ic_launcher);

        initView();

        initData();

    }

    public abstract void initView();

    public abstract void initData();

    public void initActionBar(String s, boolean b, boolean showphoto, int photo) {
        getSupportActionBar().setTitle(s);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getSupportActionBar().setDisplayHomeAsUpEnabled(b);// 显示左侧按钮(默认返回图标)

        if (showphoto) {
            getSupportActionBar().setHomeAsUpIndicator(photo);// 设置左侧图片
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        if (item.getItemId() == R.id.action_edit) {
            return true;
        }
        if (item.getItemId() == R.id.action_share) {
            return true;
        }
        if (item.getItemId() == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private ProgressDialog pd; // 等待提示框

    public void getProgress(Context context, String string) {
        pd = ProgressDialog.show(context, null, string, false, true);
    }

    public void dismissProgress() {
        pd.dismiss();
    }

    public void toast(String content) {
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
    }

    public void startActivity(View.OnClickListener onClickListener, Class<? extends Activity> targetActivity) {
        Intent intent = new Intent(BaseActivity.this, targetActivity);
        startActivity(intent);
    }

    public void startActivityAndFinish(Class<? extends Activity> targetActivity) {
        Intent intent = new Intent(BaseActivity.this, targetActivity);
        startActivity(intent);
        finish();
    }

}
